<?php
declare(strict_types=1);
namespace cms\wechat\service;

use cms\wechat\model\AuthorizeModel;
use cms\wechat\model\PlatformModel;
use Hyperf\Di\Annotation\Inject;

class AuthorizeService extends CommonService
{
    /**
     * @Inject()
     * @var AuthorizeModel
     */
    public AuthorizeModel $model;

    public function syncDataByAuth(PlatformModel $platform, array $info = [])
    {
        $data = [
            'app_id' => $info['authorization_info']['authorizer_appid'],
            'platform_id' => $platform->id
        ];
        //刷新令牌
        $data['refresh_token'] = $info['authorization_info']['authorizer_refresh_token'];
        //授权信息
        $data['func_info'] = json_encode($info['authorization_info']['func_info']);
        return $this->model->firstOrCreate([
            'app_id' => $data['app_id'],
            'platform_id' => $platform->id
        ], $data);
    }
}